/*
 * @(#)MaintDmsUpdateAlertAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.bean.UpdateAlertType;
import com.dcivision.alert.core.AlertManager;
import com.dcivision.alert.dao.UpdateAlertDAObject;
import com.dcivision.alert.dao.UpdateAlertTypeDAObject;
import com.dcivision.contact.bean.ContactPerson;
import com.dcivision.contact.bean.MtmContactListObjectLink;
import com.dcivision.contact.dao.ContactPersonDAObject;
import com.dcivision.contact.dao.MtmContactListObjectLinkDAObject;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsSendEmail;
import com.dcivision.dms.bean.DmsValidation;
import com.dcivision.dms.core.DocumentOperationManager;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.DocumentValidateManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.user.dao.UserRecordDAObject;

/**
  MaintDmsUpdateAlertAction.java
  This class is for maint update alert action
    @author          Wong Yam Lee
    @company         DCIVision Limited
    @creation date   21/08/2003
    @version         $Revision: 1.29.2.3 $
 */

public class MaintDmsUpdateAlertAction extends com.dcivision.alert.web.MaintUpdateAlertAction {

  public static final String REVISION = "$Revision: 1.29.2.3 $";

  public static final String SEPARATOR = ",";

  public MaintDmsUpdateAlertAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return ("com.dcivision.alert.dao.UpdateAlertDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return (null);
  }

  /**
   *  Method execute() - set request attributes and call super method
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    MaintDmsUpdateAlertForm maintDmsUpdateAlertForm = (MaintDmsUpdateAlertForm) form;
    String opMode = maintDmsUpdateAlertForm.getOpMode();
    String navMode = maintDmsUpdateAlertForm.getNavMode();

    // Set function code if necessary.
    if (!Utility.isEmpty(maintDmsUpdateAlertForm.getCurFunctionCode())) {
      this.setFunctionCode(maintDmsUpdateAlertForm.getCurFunctionCode());
    }

    String alertType = maintDmsUpdateAlertForm.getAlertType();
    log.debug("alertType = " + alertType);

    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);

    try {
      ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
      if (forward != null) {
//      release();
        return forward;
      }

      UpdateAlert updateAlert = null;
      UpdateAlertDAObject updateAlertDAO = new UpdateAlertDAObject(sessionContainer, conn);
      if (UpdateAlert.INVITE_TYPE.equals(maintDmsUpdateAlertForm.getAlertType())) {
        updateAlert = (UpdateAlert) updateAlertDAO.getByObjectTypeObjectIDCreatorID(GlobalConstant.OBJECT_TYPE_DOCUMENT, new Integer(maintDmsUpdateAlertForm.getObjectID())
          , sessionContainer.getUserRecordID(), UpdateAlert.INVITE_TYPE);
      } else {
        updateAlert = (UpdateAlert) updateAlertDAO.getByObjectTypeObjectIDCreatorID(GlobalConstant.OBJECT_TYPE_DOCUMENT, new Integer(maintDmsUpdateAlertForm.getObjectID())
          , sessionContainer.getUserRecordID());
      }

      if (updateAlert != null) {
        if (Utility.isEmpty(navMode)) {          
          maintDmsUpdateAlertForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
        }
        maintDmsUpdateAlertForm.setID(TextUtility.formatIntegerObj(updateAlert.getID()));
        maintDmsUpdateAlertForm.setUpdateCount(TextUtility.formatIntegerObj(updateAlert.getUpdateCount()));
      } else {
        maintDmsUpdateAlertForm = this.retrievalTargetObject(maintDmsUpdateAlertForm, request);
      }

      // Retieve the root folder document object.
      DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
      DmsDocument documentRoot = docRetrievalManager.getRootFolderByRootID(TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getRootID()));
      request.setAttribute("rootFolder", documentRoot);

      //Add By Jim Zhou       2007/01/10       EIP-1292
      Integer documentID = TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getObjectID());
      DmsDocument document = docRetrievalManager.getDocument(documentID);
      DocumentValidateManager docValidateManager = new DocumentValidateManager(sessionContainer, conn);
      StringBuffer message = new StringBuffer();

      DmsValidation validation = docValidateManager.validateOperateAction(document, false, "A");
      List lstIdMisRight = validation.getLstIdReject();
      List lstIdBeLocked = validation.getLstIdlocked();
      List lstIdBeDeleted = validation.getLstIdDeleted();
      List lstIdBeArchived = validation.getLstIdArchived();
      List lstIdRelationship = validation.getLstIdHaveRelationship();
      if (!lstIdMisRight.isEmpty() ||
          !lstIdBeDeleted.isEmpty() ||
          !lstIdBeArchived.isEmpty()){
        for (int i=0; i<lstIdMisRight.size(); i++) {
          this.addError(request,"errors.dms.no_permission",docRetrievalManager.getLocationPath((Integer)lstIdMisRight.get(i)));
        }
        for (int i=0; i<lstIdBeLocked.size(); i++) {
          this.addError(request,"errors.dms.been_checkout",docRetrievalManager.getLocationPath((Integer)lstIdBeLocked.get(i)));
        }
        for (int i=0; i<lstIdBeDeleted.size(); i++) {
          this.addError(request,"errors.dms.been_deleted",docRetrievalManager.getLocationPath((Integer)lstIdBeDeleted.get(i)));
        }
        for (int i=0; i<lstIdBeArchived.size(); i++) {
          this.addError(request,"errors.dms.been_archived",docRetrievalManager.getLocationPath((Integer)lstIdBeArchived.get(i)));
        }
        if (Utility.isEmpty(maintDmsUpdateAlertForm.getReturnTo())){
          return mapping.findForward("Home");
        } else {
          ActionForward actionForward = new ActionForward();
          actionForward.setPath("/dms/" + maintDmsUpdateAlertForm.getReturnTo());
          return actionForward;
        }
      }
      //Add ENd
      
      docRetrievalManager.release();
      updateAlertDAO = null;
      
      if (!Utility.isEmpty(opMode)) {
        if (GlobalConstant.OP_MODE_INSERT.equals(opMode)) {
          // Insert operation.
          try {
            this.insertRecord(mapping, maintDmsUpdateAlertForm, request, response);
            updateAlertDAO = new UpdateAlertDAObject(sessionContainer, conn);
            updateAlert = (UpdateAlert)updateAlertDAO.getByObjectTypeObjectID(GlobalConstant.OBJECT_TYPE_DOCUMENT,new Integer(maintDmsUpdateAlertForm.getObjectID()));
            if( updateAlert != null ) {
              Integer createrID = updateAlert.getCreatorID();
              Integer currentUserID = sessionContainer.getUserRecordID();
              if( createrID != null && !createrID.equals(currentUserID )) {
                updateAlert.setUpdateDate(new Timestamp(new Date().getTime()));
                updateAlert.setUpdaterID(currentUserID);
                updateAlert = (UpdateAlert)updateAlertDAO.updateObject(updateAlert);
                maintDmsUpdateAlertForm.setFormData(updateAlert);
               }
            }
            updateAlertDAO = null;
            
            this.handleWorkflowRoutine(mapping, maintDmsUpdateAlertForm, request, response, opMode, navMode);
            this.commit(request);
            addMessage(request, MESSAGE_RECORD_INSERT);
            //Check to see whether need send email for enternal parties
            boolean support_email=SystemParameterFactory.getSystemParameterBoolean(SystemParameterConstant.SUPPORT_EMAIL);
            if(UpdateAlert.INVITE_TYPE.equals(maintDmsUpdateAlertForm.getAlertType()) && support_email){
	          String emailSent=this.sendInvitationEmail(mapping,maintDmsUpdateAlertForm,request,response);
            }
            /*if (DmsOperationConstant.EMAIL_SENT.equals(this.sendInvitationEmail(mapping, maintDmsUpdateAlertForm, request, response))) {
              addMessage(request, "dms.label.email_send_successfully");
            }*/
          } catch (ApplicationException appEx) {
            this.rollback(request);
            handleApplicationException(request, appEx);
          }
          if (isError(request)) {
            maintDmsUpdateAlertForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
          } else {
            maintDmsUpdateAlertForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
          }
        } else if (GlobalConstant.OP_MODE_UPDATE.equals(opMode)) {
          // Update operation.
          try {
            this.updateRecord(mapping, maintDmsUpdateAlertForm, request, response);
            this.handleWorkflowRoutine(mapping, maintDmsUpdateAlertForm, request, response, opMode, navMode);
            this.commit(request);
            addMessage(request, MESSAGE_RECORD_UPDATE);
            //Check to see whether need send email for enternal parties
            boolean support_email=SystemParameterFactory.getSystemParameterBoolean(SystemParameterConstant.SUPPORT_EMAIL);
            if(UpdateAlert.INVITE_TYPE.equals(maintDmsUpdateAlertForm.getAlertType()) && support_email){
	          String emailSent=this.sendInvitationEmail(mapping,maintDmsUpdateAlertForm,request,response);
            }
            /*if (DmsOperationConstant.EMAIL_SENT.equals(this.sendInvitationEmail(mapping, maintDmsUpdateAlertForm, request, response))) {
              addMessage(request, "dms.label.email_send_successfully");
            }*/
          } catch (ApplicationException appEx) {
            this.rollback(request);
            handleApplicationException(request, appEx);
          }
          if (isError(request)) {
            maintDmsUpdateAlertForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
            return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
          } else {
            maintDmsUpdateAlertForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
          }
        } else if (GlobalConstant.OP_MODE_DELETE.equals(opMode)) {
          // Delete operation.
          try {
            this.deleteRecord(mapping, maintDmsUpdateAlertForm, request, response);
            this.handleWorkflowRoutine(mapping, maintDmsUpdateAlertForm, request, response, opMode, navMode);
            this.commit(request);
            addMessage(request, MESSAGE_RECORD_DELETE);
          } catch (ApplicationException appEx) {
            this.rollback(request);
            handleApplicationException(request, appEx);
          }
          if (isError(request)) {
            maintDmsUpdateAlertForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
            return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
          } else {
            maintDmsUpdateAlertForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
          }
        } else {
          return (super.execute(mapping, form, request, response));
        }
      } else {
        return (super.execute(mapping, form, request, response));
      }
    } catch (ApplicationException appEx) {
      throw appEx;
    } finally {
      conn=null;
    }
  }

  public void selectRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintDmsUpdateAlertForm maintDmsUpdateAlertForm = (MaintDmsUpdateAlertForm) form;
    maintDmsUpdateAlertForm = this.retrievalTargetObject(maintDmsUpdateAlertForm, request);
    List typeList = new ArrayList();
    Integer inviteTypeID = null;
    log.debug("maintDmsUpdateAlertForm.getAlertType() = " + maintDmsUpdateAlertForm.getAlertType());
    log.debug("maintDmsUpdateAlertForm.getID() = " + maintDmsUpdateAlertForm.getID());

    UpdateAlert updateAlert = null;
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    UpdateAlertDAObject updateAlertDAO = new UpdateAlertDAObject(sessionContainer, conn);
    String alertType="";
    if(!Utility.isEmpty(maintDmsUpdateAlertForm.getAlertType())){
      alertType=maintDmsUpdateAlertForm.getAlertType();
    }
    if(!Utility.isEmpty(request.getParameter("alertType"))){
      alertType=request.getParameter("alertType");
    }
    if (UpdateAlert.INVITE_TYPE.equals(alertType)) {
      updateAlert = (UpdateAlert) updateAlertDAO.getByObjectTypeObjectIDCreatorID(GlobalConstant.OBJECT_TYPE_DOCUMENT, new Integer(maintDmsUpdateAlertForm.getObjectID())
        , sessionContainer.getUserRecordID(), UpdateAlert.INVITE_TYPE);
    } else {
      updateAlert = (UpdateAlert) updateAlertDAO.getByObjectTypeObjectIDCreatorID(GlobalConstant.OBJECT_TYPE_DOCUMENT, new Integer(maintDmsUpdateAlertForm.getObjectID())
        , sessionContainer.getUserRecordID());
    }
    //update_alert table does not have clearLogs column,this value is passed from front page
    updateAlert.setClearLogs(maintDmsUpdateAlertForm.getClearLogs());
    UpdateAlertTypeDAObject updateAlertTypeDAO = new UpdateAlertTypeDAObject(sessionContainer, conn);
    if (UpdateAlert.INVITE_TYPE.equals(maintDmsUpdateAlertForm.getAlertType())) {
      typeList = updateAlertTypeDAO.getByUpdateAlertIDByOwnerID(TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getID()), UpdateAlert.INVITE_TYPE
        , sessionContainer.getUserRecordID());
    } else {
      typeList = updateAlertTypeDAO.getByUpdateAlertIDByOwnerID(TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getID()), sessionContainer.getUserRecordID());
    }
    // set action type
    if (!Utility.isEmpty(typeList)) {
      String[] actions = new String[typeList.size()];
      for (int i = 0; i < typeList.size(); ++i) {
        UpdateAlertType updateAlertType = (UpdateAlertType) typeList.get(i);
        actions[i] = updateAlertType.getActionType();
        if (i == 0) {
          updateAlert.setDueDate(updateAlertType.getDueDate());
          updateAlert.setMessage(updateAlertType.getMessage());
          inviteTypeID = updateAlertType.getID();
          updateAlert.setNotifySelf(updateAlertType.getNotifySelf());
        }
      }
      maintDmsUpdateAlertForm.setActionType(actions);
    }

    // set notify way
    if (typeList != null && typeList.size() > 0) {
      String[] asNotifyWays = TextUtility.splitString( ( (UpdateAlertType) typeList.get(0)).getNotifyWay(), this.SEPARATOR);
      maintDmsUpdateAlertForm.setNotifyWay(asNotifyWays);
    }
    
    maintDmsUpdateAlertForm.setFormData(updateAlert);
    maintDmsUpdateAlertForm.setMessage(updateAlert.getMessage());

    //set Invite related info
    if (UpdateAlert.INVITE_TYPE.equals(maintDmsUpdateAlertForm.getAlertType())) {
      AlertManager alertManager = new AlertManager(sessionContainer, conn);
      List userRecipientList = alertManager.getRecipientByUpdateAlertTypeID(inviteTypeID, UpdateAlert.USER_RECIPIENT);
      maintDmsUpdateAlertForm.setUserList( Utility.getStringArray(userRecipientList));
      List groupRecipientList = alertManager.getRecipientByUpdateAlertTypeID(inviteTypeID, UpdateAlert.GROUP_RECIPIENT);
      maintDmsUpdateAlertForm.setGroupList( Utility.getStringArray(groupRecipientList));
      List roleRecipientList = alertManager.getRecipientByUpdateAlertTypeID(inviteTypeID, UpdateAlert.ROLE_RECIPIENT);
      maintDmsUpdateAlertForm.setRoleList( Utility.getStringArray(roleRecipientList));
      List contactRecipientList = alertManager.getRecipientByUpdateAlertTypeID(inviteTypeID, UpdateAlert.CONTACT_RECIPIENT);
      maintDmsUpdateAlertForm.setContactList( Utility.getStringArray(contactRecipientList));
      List contactPersonList = alertManager.getRecipientByUpdateAlertTypeID(inviteTypeID, UpdateAlert.CONTACT_PERSON);
      maintDmsUpdateAlertForm.setContactPersonList( Utility.getStringArray(contactPersonList));
      alertManager.release();
      if (inviteTypeID!=null){
        request.setAttribute("updateAlertTypeID", inviteTypeID.toString());
      }
    }

    sessionContainer = null;
    conn = null;
    updateAlertDAO = null;
    updateAlertTypeDAO = null;

  }

  public void insertRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.insertRecord(mapping, form, request, response);
    MaintDmsUpdateAlertForm maintDmsUpdateAlertForm = (MaintDmsUpdateAlertForm) form;

    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);

    // insert record into MTM_UPDATE_ALERT_RECIPIENT
    if (UpdateAlert.ALERT_TYPE.equals(maintDmsUpdateAlertForm.getAlertType())) {
      // update record in DMS_DOCUMENT
      docOperationManager.setAlertEditID(TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getObjectID()), TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getID()));

    } else if (UpdateAlert.INVITE_TYPE.equals(maintDmsUpdateAlertForm.getAlertType())) {
      // update record in DMS_DOCUMENT
      docOperationManager.setAlertInviteID(TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getObjectID()), TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getID()));
    }
    sessionContainer = null;
    conn = null;
    docOperationManager.release();

  }
  
  public void updateRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.updateRecord(mapping, form, request, response);
    MaintDmsUpdateAlertForm maintDmsUpdateAlertForm = (MaintDmsUpdateAlertForm) form;

    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);

    // insert record into MTM_UPDATE_ALERT_RECIPIENT
    if (UpdateAlert.ALERT_TYPE.equals(maintDmsUpdateAlertForm.getAlertType())) {
      // update record in DMS_DOCUMENT
      docOperationManager.setAlertEditID(TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getObjectID()), TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getID()));

    } else if (UpdateAlert.INVITE_TYPE.equals(maintDmsUpdateAlertForm.getAlertType())) {
      // update record in DMS_DOCUMENT
      docOperationManager.setAlertInviteID(TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getObjectID()), TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getID()));
    }
    sessionContainer = null;
    conn = null;
    docOperationManager.release();

  }
  
  
  

  public String sendInvitationEmail(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
  	MaintDmsUpdateAlertForm maintDmsUpdateAlertForm = (MaintDmsUpdateAlertForm) form;
    String state = "";
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    MtmContactListObjectLinkDAObject mtmContactListObjectLinkDAO=new MtmContactListObjectLinkDAObject(sessionContainer,conn);
    
    DmsDocument document = docRetrievalManager.getDocument(TextUtility.parseIntegerObj(maintDmsUpdateAlertForm.getObjectID()));
    
    //get receipents.
    String extraRecipients=this.getExtraRecipients(form,request);
    extraRecipients=extraRecipients+maintDmsUpdateAlertForm.getRecipients();
    
    //Check for enternal parties invitation using email
    if (!Utility.isEmpty(extraRecipients) || !Utility.isEmpty(maintDmsUpdateAlertForm.getCCRecipients()) ||
        !Utility.isEmpty(maintDmsUpdateAlertForm.getBCCRecipients())) {
      DmsSendEmail email = new DmsSendEmail();
      email.setSender(UserInfoFactory.getUserEmailAddress(sessionContainer.getUserRecordID()));
      email.setRecipients(extraRecipients);
      email.setCCRecipients(maintDmsUpdateAlertForm.getCCRecipients());
      email.setBCCRecipients(maintDmsUpdateAlertForm.getBCCRecipients());
      if(!Utility.isEmpty(maintDmsUpdateAlertForm.getExternalSubject())){
        email.setSubject(maintDmsUpdateAlertForm.getExternalSubject());
      }else{
        email.setSubject(document.getDocumentName());
      }
      email.setContent(maintDmsUpdateAlertForm.getMessage());
      
      request.setAttribute("docName", document.getDocumentName());
      String version = request.getParameter("version");
      state = docOperationManager.sendDocumentByEmailAttachement(email.getSender(), email, document, version);
    }
    sessionContainer = null;
    conn = null;
    docRetrievalManager.release();
    docOperationManager.release();

    return state;
  }

  public MaintDmsUpdateAlertForm retrievalTargetObject(MaintDmsUpdateAlertForm MaintDmsUpdateAlertForm, HttpServletRequest request) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);

    // set attribute for document
    Integer objectID = TextUtility.parseIntegerObj(MaintDmsUpdateAlertForm.getObjectID());
    String objectName = "";
    String documentType = "";
    //DMS DOucment
    DmsDocument doc = docRetrievalManager.getDocument(objectID);
    if (doc != null) {
      objectName = doc.getDocumentName();
      documentType = doc.getDocumentType();
    }
    MaintDmsUpdateAlertForm.setObjectType(documentType);
    MaintDmsUpdateAlertForm.setObjectName(objectName);
    MaintDmsUpdateAlertForm.setObjectID(TextUtility.formatIntegerObj(objectID));

    sessionContainer = null;
    conn = null;
    docRetrievalManager.release();

    return MaintDmsUpdateAlertForm;
  }
  
  protected String getExtraRecipients(AbstractActionForm form,HttpServletRequest request) throws ApplicationException{
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    StringBuffer extraRecipients=new StringBuffer();

    MaintDmsUpdateAlertForm maintForm=(MaintDmsUpdateAlertForm)form; 
    MtmContactListObjectLinkDAObject mtmContactListObjectLinkDAO=new MtmContactListObjectLinkDAObject(sessionContainer,conn);
    UserRecordDAObject userRecordDAO = new UserRecordDAObject(sessionContainer, conn);
    AlertManager alertManager = new AlertManager(sessionContainer, conn);
    ContactPersonDAObject contactPersonDAO=new ContactPersonDAObject(sessionContainer,conn);
    
    if(!Utility.isEmpty(maintForm.getContactList())){
      for(int i=0;i<maintForm.getContactList().length;i++){
        Integer contactListID=TextUtility.parseIntegerObj(maintForm.getContactList()[i]);
        List userList=mtmContactListObjectLinkDAO.getObjectIDListByObjectTypeContactListID(MtmContactListObjectLink.OBJECT_TYPE_USER_RECORD,contactListID);
        List newUserList=new ArrayList();
        for(int j=0;j<userList.size();j++){
          Integer userRecordID=(Integer)userList.get(j);
          newUserList.add(userRecordID.toString());
        }
        List groupList=mtmContactListObjectLinkDAO.getObjectIDListByObjectTypeContactListID(MtmContactListObjectLink.OBJECT_TYPE_USER_GROUP,contactListID);
        List roleList=mtmContactListObjectLinkDAO.getObjectIDListByObjectTypeContactListID(MtmContactListObjectLink.OBJECT_TYPE_USER_ROLE,contactListID);
        List extraUserLsit = userRecordDAO.getIDListByLists(newUserList, groupList, roleList);
        List notifyUserIDList = alertManager.getDistinctUserIDListByLists(maintForm.getUserList(), maintForm.getGroupList(), maintForm.getRoleList());
        for(int j=0;j<extraUserLsit.size();j++){
          Integer userRecordID=(Integer)extraUserLsit.get(j);
          if(!notifyUserIDList.contains(userRecordID)){
            String userAddress=UserInfoFactory.getUserEmailAddress(userRecordID);
            if(!Utility.isEmpty(userAddress)){
              extraRecipients.append(userAddress+";");
            }
          }
        }
        List contactPersonList=mtmContactListObjectLinkDAO.getObjectIDListByObjectTypeContactListID(MtmContactListObjectLink.OBJECT_TYPE_CONTACT_PERSON,contactListID);
        for(int j=0;j<contactPersonList.size();j++){
          Integer contactPersonID=(Integer)contactPersonList.get(j);
          ContactPerson contactPerson=(ContactPerson)contactPersonDAO.getObjectByID(contactPersonID);
          if(!Utility.isEmpty(contactPerson.getEmail())){
            extraRecipients.append(contactPerson.getEmail()+";");
          }         
        }
      }
    }
    if(!Utility.isEmpty(maintForm.getContactPersonList())){
      for(int i=0;i<maintForm.getContactPersonList().length;i++){
        ContactPerson contactPerson = (ContactPerson)contactPersonDAO.getObjectByID(TextUtility.parseIntegerObj(maintForm.getContactPersonList()[i]));
        if(extraRecipients.toString().indexOf(contactPerson.getEmail())<0){
          extraRecipients.append(contactPerson.getEmail()+";");
        }
      }
    }
    return extraRecipients.toString();   
  }
  

}